package com.letv.android.client.episode.request;

import android.content.Context;

import com.ledim.app.LeDimApplication;
import com.letv.android.client.episode.fragment.TabsFragment;
import com.letv.android.client.episode.parser.TabCombineEpisodeParser;
import com.letv.android.client.episode.parser.TabTopicCombineEpisodeParser;
import com.letv.core.api.LetvRequest;
import com.letv.core.api.MediaAssetApi;
import com.letv.core.bean.DataHull;
import com.letv.core.bean.TabAllDataBean;
import com.letv.core.bean.TabTopicAllDataBean;
import com.letv.core.network.volley.VolleyRequest;
import com.letv.core.network.volley.VolleyResponse.CacheResponseState;
import com.letv.core.network.volley.VolleyResponse.NetworkResponseState;
import com.letv.core.network.volley.toolbox.SimpleResponse;
import com.letv.core.network.volley.toolbox.VolleyDiskCache;

public class RequestCombineDataTask {
    private Context mContext;
    private ICommonRequestCallback mCallback;
    private ICommonRequestCallback mControllerCallback;
    private String mCid;
    private String mZid;
    private String mVid;
    private String mPid;

    private int mLaunchMode;

    public RequestCombineDataTask(Context context) {
        mContext = context;
    }

    public void setCallBack(ICommonRequestCallback callback) {
        mCallback = callback;
    }

    public void setControllerCallback(ICommonRequestCallback callback) {
        mControllerCallback = callback;
    }

    public void setParams(String cid, String zid, String vid, String pid) {
        mZid = zid;
        mVid = vid;
        mPid = pid;
        mCid = cid;
    }

    public void setLaunchMode(int launchMode) {
        mLaunchMode = launchMode;
    }

    public void start() {

        if (mLaunchMode == TabsFragment.LAUNCH_MODE_ALBUM) {
            /*
	         * 点播请求合并接口数据
	         */
            new LetvRequest<TabAllDataBean>(mContext, TabAllDataBean.class)
                    //电视剧
//          .setUrl("http://api.mob.app.letv.com/play/tabs?pcode=010110000&version=5.9&devid=fc679b870ca10fb123999b785f2574d2&cid=0&zid=0&vid=0&pid=10006909")

                    //电影
//          .setUrl("http://api.mob.app.letv.com/play/tabs?pcode=010110000&version=5.9&devid=fc679b870ca10fb123999b785f2574d2&cid=0&zid=&vid=0&pid=10004877")

                    //动漫
//          .setUrl("http://api.mob.app.letv.com/play/tabs?pid=91890&pcode=010110000&vid=2059683&cid=0&devid=fc679b870ca10fb123999b785f2574d2&version=5.9")

                    //综艺
//          .setUrl("http://api.mob.app.letv.com/play/tabs?pcode=010110000&version=5.9&devid=fc679b870ca10fb123999b785f2574d2&cid=0&zid=&vid=22938315&pid=10008206")

                    //相关
//          .setUrl("http://api.mob.app.letv.com/play/tabs?pcode=010110000&version=5.9&devid=fc679b870ca10fb123999b785f2574d2&cid=0&zid=&vid=22990005&pid=10010165")

                    .setCache(new VolleyDiskCache(mContext, "requestTabAlbumAllData" + mCid + mZid + mVid + mPid))
                    .setParser(new TabCombineEpisodeParser())
                    .setCallback(new SimpleResponse<TabAllDataBean>() {

                        @Override
                        public void onNetworkResponse(VolleyRequest<TabAllDataBean> request, TabAllDataBean result,
                                                      DataHull hull, NetworkResponseState state) {
                            if (mControllerCallback != null) {
                                mControllerCallback.onNetworkResponse(request, result, hull, state);
                            }

                            if (mCallback != null) {
                                mCallback.onNetworkResponse(request, result, hull, state);
                            }
                        }

                        @Override
                        public void onCacheResponse(VolleyRequest<TabAllDataBean> request, TabAllDataBean result,
                                                    DataHull hull, CacheResponseState state) {

                            if (mControllerCallback != null) {
                                mControllerCallback.onCacheResponse(request, result, hull, state);
                            }
                            
                            if (mCallback != null) {
                                mCallback.onCacheResponse(request, result, hull, state);
                            }

                            request.setUrl(MediaAssetApi.getInstance(LeDimApplication.getInstance())
                                                        .getCombineTabDataUrl(mContext, "0", mCid, mZid, mVid, mPid));
                        }

                        @Override
                        public void onErrorReport(VolleyRequest<TabAllDataBean> request, String errorInfo) {
                            super.onErrorReport(request, errorInfo);

                            if (mControllerCallback != null) {
                                mControllerCallback.onErrorReport(request, errorInfo);
                            }
                            
                            if (mCallback != null) {
                                mCallback.onErrorReport(request, errorInfo);
                            }
                        }

                    }).add();
        } else {
	        /*
	         * 专题请求合并接口数据
	         */
            new LetvRequest<TabTopicAllDataBean>(mContext, TabTopicAllDataBean.class)
                    .setCache(new VolleyDiskCache(mContext, "requestTabTopicAllData" + mCid + mZid + mVid + mPid))
                    .setParser(new TabTopicCombineEpisodeParser())
                    .setCallback(new SimpleResponse<TabTopicAllDataBean>() {

                        @Override
                        public void onNetworkResponse(VolleyRequest<TabTopicAllDataBean> request, TabTopicAllDataBean result,
                                                      DataHull hull, NetworkResponseState state) {
                            
                            if (mControllerCallback != null) {
                                mControllerCallback.onNetworkResponse(request, result, hull, state);
                            }
                            
                            if (mCallback != null) {
                                mCallback.onNetworkResponse(request, result, hull, state);
                            }                            
                        }

                        @Override
                        public void onCacheResponse(VolleyRequest<TabTopicAllDataBean> request, TabTopicAllDataBean result,
                                                    DataHull hull, CacheResponseState state) {

                            if (mControllerCallback != null) {
                                mControllerCallback.onCacheResponse(request, result, hull, state);
                            }
                            
                            if (mCallback != null) {
                                mCallback.onCacheResponse(request, result, hull, state);
                            }
                            
                            request.setUrl(MediaAssetApi.getInstance().getCombineTabDataUrl(mContext,"0", mCid, mZid, mVid, mPid));
                        }

                        @Override
                        public void onErrorReport(VolleyRequest<TabTopicAllDataBean> request, String errorInfo) {
                            super.onErrorReport(request, errorInfo);
                            if (mControllerCallback != null) {
                                mControllerCallback.onErrorReport(request, errorInfo);
                            }
                            if (mCallback != null) {
                                mCallback.onErrorReport(request, errorInfo);
                            }
                        }

                    }).add();
        }
    }

}
